package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.MemberMapper;
import com.xr.mapper.OrderMapper;
import com.xr.mapper.OrderSettingMapper;
import com.xr.pojo.*;
import com.xr.result.Result;
import com.xr.service.OrderService;
import com.xr.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    @Autowired
    private MemberMapper memberMapper;


    @Override
    public Map<String, Object> queryAll(Integer id) {
        return orderMapper.findOrderDetailById(id);
    }

    @Override
    public Order findById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public Result addOrder(Map<String, String> map) throws CustomException {

        String orderDate = map.get("orderDate");
        try {
            Date date = DateUtils.parseString2Date(orderDate);
            OrderSetting orderSetting = orderSettingMapper.findCountByOrderDate(date);

            //1.t_ordersetting 判断预约设置信息是否存在，条件：orderDate
            if(StringUtils.isEmpty(orderSetting.getOrderdate())){

                return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);

            }
            //判断人数是否约满 约满：就不能预约 没满：就可以预约，更新已预约人数的数量
            if (orderSetting.getNumber()<orderSetting.getReservations()){
                return  new Result(false,MessageConstant.ORDER_FULL);

            }
            //2.t_member 判断是否为会员，条件：通过手机号 非会员，添加会员到会员表：
            String telephone = map.get("telephone");
            MemberExample me = new MemberExample();
            MemberExample.Criteria mec = me.createCriteria();
            mec.andPhonenumberEqualTo(telephone);
            List<Member> members = memberMapper.selectByExample(me);
//            Member member = new Member();
            if(members!=null && members.size()>0){
                Integer setmealId = Integer.parseInt(map.get("setmealId"));

                System.out.println(members.get(0).getId()+""+date+""+setmealId+"left join");
                //3.t_order 判断是否有重复预约，
                // 条件：member_id,orderDate,setmeal_id 重复预约就要报错 不重复
                Order order = new Order(members.get(0).getId(),date,setmealId);
                System.out.println(order+"right---join2");
                List<Order> orderList = orderMapper.findByCondition(order);
                System.out.println(orderList+"right---join");
                //3.t_order 判断是否有重复预约
                if(orderList != null && orderList.size() > 0){
                    throw new CustomException(MessageConstant.HAS_ORDERED);
                }

            }
//            else{
//                member.setName(map.get("name"));
//                member.setPhonenumber(telephone);
//                member.setIdcard(map.get("idCard"));
//                member.setSex(map.get("sex"));
//                member.setRegtime(new Date());
//                memberMapper.insertSelective(member);
//            }
            orderSetting.setReservations(orderSetting.getReservations()==null?1:orderSetting.getReservations()+1);
            System.out.println(orderSetting+"below join");

            orderSettingMapper.updateOrderSetting(orderSetting);

            System.out.println(members+"yyyyyy");
            Member member = new Member();
            if(!StringUtils.isEmpty(members)){
                System.out.println("aaaa");
//                Member member = new Member();

                member.setName(map.get("name"));
                member.setPhonenumber(telephone);
                member.setIdcard(map.get("idCard"));
                member.setSex(map.get("sex"));
                member.setRegtime(new Date());
                memberMapper.insertSelective(member);
            }
            System.out.println(members+"yyyyyy");
            Order order = new Order(member.getId(),date,"微信预约","未到诊",Integer.parseInt(map.get("setmealId")));
            orderMapper.insertSelective(order);
            return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false,MessageConstant.ORDERSETTING_FAIL);
    }
}
